//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Drawing.Internals;

namespace OpenLS.Drawing
{
    internal static class SerializationHelper
    {
        public static double GetOptionalPercent(ReadContext c, string name, int defaultValue)
        {
            double d = c.GetOptionalInteger(name, defaultValue);
            return d/1000d;
        }

        public static void SetOptionalPercent(WriteContext c, string name, double value, int defaultValue)
        {
            int i = (int) Math.Round(value*1000);
            c.SetOptionalInteger(name, i, defaultValue);
        }

        internal static double GetOptionalPositiveCoordinate(ReadContext c, string attributeName, int defaultValue)
        {
            int i = c.GetOptionalInteger(attributeName, defaultValue);
            return EmuConverter.FromEmu(i);
        }

        internal static double GetOptionalPositiveFixedAngle(ReadContext c, string attributeName, int defaultValue)
        {
            double i = c.GetOptionalInteger(attributeName, defaultValue);
            return i/60000;
        }

        internal static void SetOptionalPositiveCoordinate(WriteContext c, string attributeName, double value,
                                                           int defaultValue)
        {
            c.SetOptionalInteger64(attributeName, EmuConverter.ToEmu(value), defaultValue);
        }

        internal static void SetOptionalPositiveFixedAngle(WriteContext c, string attributeName, double direction,
                                                           int defaultValue)
        {
            c.SetOptionalInteger(attributeName, (int) Math.Round(direction*60000), defaultValue);
        }

        internal static double GetFontSize(ReadContext c, string attributeName)
        {
            double i = c.GetInteger(attributeName);
            return i/100;
        }

        internal static void SetFontSize(WriteContext c, string attributeName, double value)
        {
            int i = (int) Math.Round(value*100);
            c.SetInteger(attributeName, i);
        }
    }
}